package com.mp.controller;

import java.util.Optional;

import javax.annotation.Resource;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey.Option;
import com.mp.model.User;
import com.mp.service.IUserService;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;

@RestController
@EnableTransactionManagement
public class UserController {
	
	@Resource
	private IUserService userService;
	@Resource
	private RedisTemplate<?, ?> redisTemplate;
	@Resource
	private JedisPoolConfig redisPoolConfig;
	@Resource
	private JedisConnectionFactory connectionFactory;
	@Resource
	private Jedis jedis;
	
	@RequestMapping("user/{username}")
	public User getUserByUsername(@PathVariable String username){
		User user = userService.selectUserByUsername(username);
		System.out.println(user);
		System.out.println(connectionFactory.getHostName() + connectionFactory.getPort());
		jedis.set("user", "bbb");
		System.out.println(jedis.get("user"));
		
		return user;
	}
	
	@KafkaListener(topics="testTopic")
	public void listener(ConsumerRecord<?, ?> record){
		Optional<?> message = Optional.ofNullable(record);
		if (message.isPresent()) {
			System.out.println(message.get());
		}
	}
	
}
